<?php defined('SYSPATH') or die('No direct script access.');

class Belanjastok_Model extends Model {
	
	public function insert_belanjastok($id_stok, $tanggal, $harga_beli, $jumlah) {
		$pembukuan_model = new Pembukuan_Model;
		$id = $pembukuan_model->insert_pembukuan($tanggal,0,$harga_beli*$jumlah,'stok');
		$data = array('ID_stok'=>$id_stok, 'ID_pembukuan'=>$id, 'tanggal'=>$tanggal, 'harga_beli'=>$harga_beli, 'jumlah'=>$jumlah);
		$this->db->insert('belanjastok',$data);
	}
	
	public function get_all_belanjastok() {
		return ($this->db->from('belanjastok')->orderby('ID_belanjastok')->get());
	}
	
	public function get_belanjastok($id_belanjastok) {
		return $this->db->from('belanjastok')->where('ID_belanjastok',$id_belanjastok)->get();
	}
	
	public function get_belanjastok_by_id_pembukuan($id_pembukuan) {
		return $this->db->from('belanjastok')->select(
			'belanjastok.ID_belanjastok AS belanjastok_ID_belanjastok',
			'belanjastok.ID_pembukuan AS belanjastok_ID_pembukuan',			
			'belanjastok.ID_stok AS belanjastok_ID_stok',
			'belanjastok.tanggal AS belanjastok_tanggal',
			'belanjastok.harga_beli AS belanjastok_hargabeli',
			'belanjastok.jumlah AS belanjastok_jumlah',
			'stok.nama AS stok_nama',
			'stok.unit AS stok_unit'
		)->join('stok','stok.ID_stok','belanjastok.ID_stok')->where('ID_pembukuan',$id_pembukuan)->get()->current();
	}
}